from apps.users.models import User
from datetime import date, timedelta
from rest_framework.views import APIView
from rest_framework.response import Response


# 日活用户
class DailyActiveView(APIView):
    """
    日活用户
    1.查询用户最后一次登录的时间是今天
    2. 只要用户在当天登录过一次就算是日活用户
    """

    def get(self, request):
        # 1.获取当天的日期
        today = date.today()
        # 2.过滤查询
        # today  是一个日期              年月日
        # last_login 是一个日期时间       年月日 时分秒

        # last_login__gte=today
        # django 会把today 变为 年月日 00:00:00
        count = User.objects.filter(last_login__gte=today).count()
        # count=User.objects.filter(last_login=today).count()
        # 3.返回数量
        return Response({'count': count})


# class DailyActiveView(APIView):
#     def get(self, request):
#         # 获取当天时间
#         today = date.today()
#         # 过滤查询
#         count = User.objects.filter(last_login__gte=today).count()
#         # 返回
#         return Response({'count': count})


# 日下单用户量
class DailyOrderCountAPIView(APIView):
    def get(self, request):
        # 获取当日时间
        today = date.today()
        # 过滤查询
        count = User.objects.filter(last_login__gte=today).count()
        # 返回
        return Response({'count': count})

# class DailyOrderCountAPIView(APIView):
#     def get(self, request):
#         # 获取当日时间
#         today = date.today()
#         # 过滤查询
#         count = User.objects.filter(last_login__gte=today).count()
#         # 返回
#         return Response({'count': count})


# 月增用户统计
class MonthAPIView(APIView):
    def get(self, request):
        # 1.确定数据形式
        data = []
        # 2.确定日期
        today = date.today()
        # 确定开始时间
        start_date = today - timedelta(days=30)
        # 3.遍历获取某一天的用户增量
        for i in range(30):
            index = start_date + timedelta(days=i)
            after = start_date + timedelta(days=i+1)

            count = User.objects.filter(date_joined__gte=index, date_joined__lt=after).count()

            data.append({
                "count": count,
                'date': index
            })
        return Response(data)


# class MonthAPIView(APIView):
#     def get(self, request):
#         # 1.确定数据形式
#         data = []
#         # 2.确定日期
#         today = date.today()
#         # 确定开始时间
#         start_date = today - timedelta(days=30)
#         # 3.遍历获取某一天的用户增量
#         for i in range(30):
#             index = start_date + timedelta(days=i)
#             after = start_date + timedelta(days=i+1)
#
#             count = User.objects.filter(date_joined__gte=index, date_joined__lt=after).count()
#
#             data.append({
#                 "count": count,
#                 "date": index
#             })
#         return Response(data)

# 用户总量统计
class TotleCountAPIView(APIView):

    def get(self, request):

        count = User.objects.all().count()
        # 获取时间
        today = date.today()

        return Response({'count': count, 'date': today})


# 日增用户统计
class DayIncrementAPIView(APIView):
    def get(self, request):

        # 获取时间
        today = date.today()
        # 获取总数
        count = User.objects.filter(date_joined__gte=today).count()
        return Response({'count': count, 'date': today})

